# coding=utf-8
import unittest
import time
from common import HTMLTestRunner_api
from email.header import Header
from email.mime.text import MIMEText
import smtplib
from email.mime.multipart import MIMEMultipart
from common.readexcel import *

curpath = os.path.dirname(os.path.realpath(__file__))
'''/Users/chengyanan/Desktop/excelddtdriver'''
report_path = os.path.join(curpath, "report")
'''/Users/chengyanan/Desktop/excelddtdriver/report'''

if not os.path.exists(report_path): os.mkdir(report_path)
case_path = os.path.join(curpath, "case")
'''/Users/chengyanan/Desktop/excelddtdriver/case'''


def add_case(casepath=case_path, rule="test_kuai8.py"):
    '''加载所有的测试用例'''
    # 定义discover方法的参数
    discover = unittest.defaultTestLoader.discover(casepath,
                                                   pattern=rule, )

    return discover


def send_mail():
    '''
        发送邮件内容
  :param to_user:发送邮件的人
  :param sub:主题
  :param content:邮件内容
        '''
    smtpserver = 'smtp.qq.com'  # 发件服务器
    port = 465
    send_user = '316835981@qq.com'  # 发送端
    to_user = ['13439875784@163.com', 'lidonghui@yixia.com']  # 接收端
    # =========编辑邮件内容=========
    report_dir = report_path + r"/result.html"
    f = open(report_dir, 'rb')
    mail_body = f.read()
    f.close()
    msg = MIMEMultipart()
    msg['Subject'] = '接口自动化测试报告'  # 主题
    msg['From'] = send_user  # 发件人
    msg['To'] = ','.join(to_user)  # 收件人
    text = MIMEText(mail_body, 'html', 'utf-8')
    text['Subject'] = Header('接口自动化自动化测试报告', 'utf-8')  # 定义文件正文标题
    msg.attach(text)
    # -------附件------------
    att = MIMEText(mail_body, "base64", "utf-8")
    att["Content-Type"] = "application/octet-stream"
    att.add_header('Content-Disposition', 'attachment', filename='接口自动化测试报告.html')  # 定义附件名称
    msg.attach(att)  # 添加附件

    # 发送xlsx

    kuai8_result = report_path + r"/kuai8_result.xlsx"
    att2 = MIMEText(open(kuai8_result, 'rb').read(), 'base64', 'utf-8')
    att2["Content-Type"] = 'application/octet-stream'
    att2.add_header('Content-Disposition', 'attachment', filename='接口测试具体报告.xlsx')
    msg.attach(att2)

    # 发送
    server = smtplib.SMTP_SSL(smtpserver, port)
    server.login(send_user, 'zbfbslvwmlprbjhb')
    server.sendmail(send_user, to_user, msg.as_string())
    server.close()


def run_case(all_case, reportpath=report_path):
    '''执行所有的用例, 并把结果写入测试报告'''
    htmlreport = reportpath + r"/result.html"
    print("测试报告生成地址：%s" % htmlreport)
    fp = open(htmlreport, "wb")
    runner = HTMLTestRunner_api.HTMLTestRunner(stream=fp,
                                               verbosity=2,
                                               title="快8接口自动化测试报告",
                                               description="用例执行情况")

    # 调用add_case函数返回值
    runner.run(all_case)
    fp.close()
    time.sleep(5)

    if run_success_result():
        print("全部通过")
    else:
        print('有失败的')
        send_mail()


if __name__ == "__main__":
    cases = add_case()
    run_case(cases)
